package com.hcy.driverapi.controller;

import com.hcy.common.dto.request.PushMsgRequestDTO;
import com.hcy.common.dto.response.MessageResponseDTO;
import com.hcy.commonn.CommonInfoClient;
import com.hcy.driver.DriverBaseInfoClient;
import com.hcy.driver.DriverPageInfoClient;
import com.hcy.driver.dto.request.BaseInfoRequestDTO;
import com.hcy.driver.dto.response.BaseInfoResponseDTO;
import com.hcy.orderclient.OrderForDriverClient;
import com.hcy.ordermodel.dto.request.HomeInfoRequestDTO;
import com.hcy.ordermodel.dto.response.HomeInfoResponseDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.model.ReturnResult;
import com.hcy.rabbitbasicmodel.signl.Query;
import com.hcy.rabbitbasicutils.utils.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author huchenying
 * @Description 司机端主页数据
 * @Date 2021/12/20 11:56
 */
@RestController
@RequestMapping("/api")
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    DriverPageInfoClient driverPageInfoClient;
    @Autowired
    DriverBaseInfoClient driverBaseInfoClient;
    @Resource
    OrderForDriverClient orderForDriverClient;
    @Autowired
    CommonInfoClient commonInfoClient;
    @Autowired
    RedisUtils redisUtils;
    /**
     * @Author huchenying
     * @Description 今日单量，今日流水  当前服务单 以及预约单
     * @Date 2021/12/20 11:57
     **/
    @PostMapping("/homeInfo")
    public ReturnResult homeInfo(@Validated @RequestBody HomeInfoRequestDTO requestDTO) {
        logger.info("homeInfo start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        //判断有没有接单,有就带上参数 orderNumber
        String driverRev = redisUtils.getDriverRev(requestDTO.getDriverId().toString());
        if(StringUtils.isNotEmpty(driverRev)){
            //当前司机有接单
            logger.info("当前司机有接单 orderNumber -> {}",driverRev);
            requestDTO.setOrderNumber(driverRev);
        }
        ResponseData<HomeInfoResponseDTO> responseData = orderForDriverClient.findDriverHomeInfo(requestDTO);
        if (responseData.getCode() == Code.Success) {
            return ReturnResult.success().setData(responseData.getData());
        } else {
            logger.error("请求失败");
            return ReturnResult.fail("系统异常");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取终端基础信息 包括 司机信息 高德相关信息  key,sid,tid
     * @Date 2021/12/20 12:00
     **/
    @PostMapping("/baseInfo")
    public ReturnResult baseInfo(@RequestBody @Validated BaseInfoRequestDTO requestDTO) {
        ResponseData<BaseInfoResponseDTO> responseData = driverPageInfoClient.baseInfo(requestDTO);
        if(responseData.getCode() == Code.Success){
            return ReturnResult.success().setData(responseData.getData());
        }
        return ReturnResult.fail("基础信息查询失败！");
    }

    /**
     * @Author huchenying
     * @Description 司机出车
     * @Date
     **/
    @PostMapping("/driverStart")
    public ReturnResult driverStart(@RequestBody @Validated BaseInfoRequestDTO requestDTO) {
        logger.info("driverStart start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
//        有问题
//        ResponseData<Object> responseData = driverBaseInfoClient.driverStart(requestDTO);
//        if(responseData.getCode() == Code.Success){
//            return ReturnResult.success().setData(responseData.getData());
//        }
//        return ReturnResult.fail("出车失败！");
        return ReturnResult.success();
    }
    /**
     * @Author huchenying
     * @Description 司机出车
     * @Date
     **/
    @PostMapping("/driverEnd")
    public ReturnResult driverEnd(@RequestBody @Validated BaseInfoRequestDTO requestDTO) {
        logger.info("driverEnd start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
//        ResponseData<Object> responseData = driverBaseInfoClient.driverEnd(requestDTO);
//        if(responseData.getCode() == Code.Success){
//            return ReturnResult.success().setData(responseData.getData());
//        }
//        return ReturnResult.fail("收车车失败！");
        return ReturnResult.success();
    }

    /**
     * @Author huchenying
     * @Description 查询消息列表
     * @Date 2022/4/13 18:54
     **/
    @PostMapping("/getMessageList")
    public ReturnResult getMessageList(@RequestBody @Validated(Query.class) PushMsgRequestDTO requestDTO){
        logger.info("getMessageList start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<List<MessageResponseDTO>> listResponseData = commonInfoClient.queryMessage(requestDTO);
            return ReturnResult.success().setData(listResponseData);
        } catch (Exception e) {
            logger.error("e -> {}",ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("消息列表获取失败！");
        }
    }
}
